{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from math import sqrt\n",
    "from collections import Counter\n",
    "import random as rd\n",
    "import matplotlib as mpl\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "%run kNN.py"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n"
     ]
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXQAAAD8CAYAAABn919SAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4wLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvqOYd8AAAErBJREFUeJzt3W+MZXd93/H3Z1lvzLrxgrvDJsHeHYsCVYKDIUNEoAnGSyNLAZwHURW6bk2DMhIPCFgUhFkJy6lWQoAao0ZKNMLO0rJ1Ao6TAG0T3G29VqVgNOvYrP8lrhrverGdHWN1W2UF2OLbB3PHtYeZnbl/5p65v3m/pNXce+bsnI9XMx9/59xzfydVhSRp8m3rOoAkaTQsdElqhIUuSY2w0CWpERa6JDXCQpekRqxZ6EluS3ImyYMv2nZlkm8muT/JfJKf39iYkqS1rGdCPwxcs2zbZ4Cbq+pK4FO955KkDq1Z6FV1D/Ds8s3Axb3Hu4AnR5xLktSn7QP+vY8Af5Hkcyz+T+Ft6/lLu3fvrunp6QEPKUlb0/Hjx5+pqqm19hu00D8I3FBVf5zknwG3Au9aaccks8AswN69e5mfnx/wkJK0NSU5uZ79Br3K5Xrgzt7jrwCrvihaVXNVNVNVM1NTa/4PRpI0oEEL/UngHb3HVwOPjSaOJGlQa55ySXI7cBWwO8lp4CbgN4HPJ9kOfI/eKRVJUnf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TuBtHWfq198l+UmA3sdN9+vz+SR5P/Bu4EBNzjXCr2FxCHig9zN7KXBfkp/oNNX6nQburEXfYvGswKZ8UXeZ61n8GQX4CrA5XxStqhur6tKqmmbxRbn/VlUTMSlW1dPAE0le39u0H3i4w0j9OAW8NcnO3oSynwl5QfdFvsriNzq9j3/WYZa+JLmGxdOM762qc13nWa+qOlFVr6qq6d7P7Gngzb2fhUnwp8A7AZK8DtjBZCzW9STwjt7jq4HH+v4KVTXWP8BVwNfHfdwhM18JzAPfZvGb5ZVdZ+oj+83Ao8CDwH8AfqzrTOfJejuL5/qfY7FEPgD8QxavbnkM+K/AJV3n7CP7/wSeAO7v/fn9rnOuN/uyzz8O7O46Zx//7juAL/W+5+8Dru465zpz/xPgOPAAcC/wc/1+Xd8pKkmN8J2iktQIC12SGmGhS1IjLHRJaoSFLkmNsNAlqREWuiQ1wkKXpEb8Pz92m6J8fuQIAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "xtype1 = [[rd.uniform(1, 16), rd.uniform(1, 11)] for _ in range(10)]\n",
    "xtype2 = [[rd.uniform(11, 21), rd.uniform(9, 21)] for _ in range(10)]\n",
    "\n",
    "yraw1 = [0 for _ in range(10)]\n",
    "yraw2 = [1 for _ in range(10)]\n",
    "\n",
    "X_train = np.vstack((xtype1, xtype2))\n",
    "y_train = np.hstack((yraw1, yraw2))\n",
    "\n",
    "x = np.array([rd.uniform(1, 21) for _ in range(2)])\n",
    "\n",
    "predict_y = kNN_classify(6, X_train, y_train, x)\n",
    "print(predict_y)\n",
    "\n",
    "plt.scatter(X_train[:10, 0], X_train[:10, 1], c='red', marker=\"+\")\n",
    "plt.scatter(X_train[11:, 0], X_train[11:, 1], c=\"green\", marker=\"o\")\n",
    "plt.scatter(x[0], x[1], c='y', marker='*')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.neighbors import KNeighborsClassifier"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "kNN_classifier = KNeighborsClassifier(n_neighbors=6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "KNeighborsClassifier(algorithm='auto', leaf_size=30, metric='minkowski',\n",
       "           metric_params=None, n_jobs=None, n_neighbors=6, p=2,\n",
       "           weights='uniform')"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "kNN_classifier.fit(X_train, y_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[13.25462496  2.7061733 ]]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([0])"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print(x.reshape(1, -1))\n",
    "kNN_classifier.predict(x.reshape(1, -1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
